package com.hunan.api.catv.service.impl.smartfamily;

import com.google.gson.JsonObject;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2021/11/15 0015
 * @package com.hunan.api.catv.service.impl.smart_family
 * @company 湖南有线
 */
@Service
public class SmartService {

    private static Logger log = LoggerFactory.getLogger(SmartService.class);
    @Autowired
    RestTemplate restTemplate;


    public JsonObject getToken(String domain, MultiValueMap<String, Object> body) {
        try {
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body,getHeader());
            ResponseEntity<String> response = restTemplate.postForEntity(domain + "/getToken", request, String.class);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("获取token失败:{}", e);
        }
        return null;
    }

    public HttpHeaders getHeader() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
//        headers.add("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE);
        return headers;
    }
    public HttpHeaders getHeader(String  token) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        headers.add("token", token);
//        headers.add("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE);
        return headers;
    }

    public JsonObject simpleInfo(String domain, MultiValueMap<String, Object> paraMap, Object token) {
        try {
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(paraMap,getHeader((String) token));
            ResponseEntity<String> response = restTemplate.postForEntity(domain + "/msg/info", request, String.class);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("获取蜗牛智家消息读取状态失败:{}", e);
        }
        return null;
    }

    public JsonObject msgList(String domain, MultiValueMap<String, Object> paraMap, Object token) {
        try {
            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(paraMap,getHeader((String) token));
            ResponseEntity<String> response = restTemplate.postForEntity(domain + "/msg/list", request, String.class);
            if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
                return GsonUtils.fromJson(response.getBody(), JsonObject.class);
            }
            log.info("远程调用非200返回{}", response.getBody());
        } catch (Exception e) {
            log.error("获取蜗牛智家消息详细列表失败:{}", e);
        }
        return null;
    }

}
